package com.instalinks.android.smspostservice.activity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.instalinks.android.lib.AppHelper;
import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.utils.enums.TimeOuts;

public class InfoActivity extends AppTabBaseActivity {

	private Handler mHandler;
	private LinearLayout layoutAboutData;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.initialize();
	}

	private void initialize() {
		this.setContentView(R.layout.info);

		layoutAboutData = (LinearLayout) this
				.findViewById(R.id.layoutAboutData);

		mHandler = new Handler();
		mHandler.removeCallbacks(statusUpdater);
		mHandler.post(statusUpdater);
	}

	private void addAttribute(String key, String value) {
		LayoutInflater li = (LayoutInflater) getApplicationContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = li.inflate(R.layout.layout_key_value, null);

		v.setTag(key + "~~" + value);
		((TextView) v.findViewById(R.id.txtKey)).setText(key);
		((TextView) v.findViewById(R.id.txtValue)).setText(value);

		layoutAboutData.addView(v, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
	}

	private void setValues() {
		mAppCfg.rePopulate();

		layoutAboutData.removeAllViews();

		addAttribute("Device ID", mAppCfg.getDeviceId());
//		addAttribute("Subscriber ID", mAppCfg.getSubscriberId());
		addAttribute("Battery", mAppCfg.getBatteryStatus());

		if (AppHelper.isNumeric(mAppCfg.getSignalStrength())) {
			int val = -113 + (2 * Integer.valueOf(mAppCfg.getSignalStrength()));
			addAttribute("Signal", String.valueOf(val) + " dB");
		} else {
			addAttribute("Signal", mAppCfg.getSignalStrength());
		}

		addAttribute("Network Status",
				(mAppCfg.isConnectedToNetwork() ? "Connected" : "Disconnected"));

		addAttribute("Network Type", mAppCfg.getNetworkType().toString());
		addAttribute("Email", mAppCfg.getEmail());
		addAttribute("Config Reloaded On", mAppCfg.getConfigUpdateDateTime());

		String versionName = "";
		try {
			PackageInfo pInfo = this.getApplicationContext().getPackageManager().getPackageInfo(
					this.getApplicationContext().getPackageName(), 0);
			versionName = pInfo.versionName;//+ ", " + pInfo.versionCode;
		} catch (NameNotFoundException e) {
		}
		
		addAttribute("App Version", versionName);
//		addAttribute("Registration ID", mAppCfg.getRegistrationId());
	}

	private Runnable statusUpdater = new Runnable() {

		@Override
		public void run() {
			setValues();
			mHandler.postDelayed(this, TimeOuts.TwoSec.getValue());
		}
	};

	@Override
	protected boolean doBackgroundWork() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void doOnSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doOnFailure() {
		// TODO Auto-generated method stub
		
	}
}
